/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;

import Logica.CategoriaEmp;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Logica.Empleado;
import Persistencia.exceptions.NonexistentEntityException;
import Persistencia.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Pablo
 */
public class CategoriaEmpJpaController implements Serializable {
    
    public CategoriaEmpJpaController(){
        emf = Persistence.createEntityManagerFactory("TpFinalPU");
    }

    public CategoriaEmpJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CategoriaEmp categoriaEmp) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Empleado unEmpleado = categoriaEmp.getUnEmpleado();
            if (unEmpleado != null) {
                unEmpleado = em.getReference(unEmpleado.getClass(), unEmpleado.getNroDoc());
                categoriaEmp.setUnEmpleado(unEmpleado);
            }
            em.persist(categoriaEmp);
            if (unEmpleado != null) {
                CategoriaEmp oldUnaCategoriaEmpOfUnEmpleado = unEmpleado.getUnaCategoriaEmp();
                if (oldUnaCategoriaEmpOfUnEmpleado != null) {
                    oldUnaCategoriaEmpOfUnEmpleado.setUnEmpleado(null);
                    oldUnaCategoriaEmpOfUnEmpleado = em.merge(oldUnaCategoriaEmpOfUnEmpleado);
                }
                unEmpleado.setUnaCategoriaEmp(categoriaEmp);
                unEmpleado = em.merge(unEmpleado);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCategoriaEmp(categoriaEmp.getNombre()) != null) {
                throw new PreexistingEntityException("CategoriaEmp " + categoriaEmp + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CategoriaEmp categoriaEmp) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CategoriaEmp persistentCategoriaEmp = em.find(CategoriaEmp.class, categoriaEmp.getNombre());
            Empleado unEmpleadoOld = persistentCategoriaEmp.getUnEmpleado();
            Empleado unEmpleadoNew = categoriaEmp.getUnEmpleado();
            if (unEmpleadoNew != null) {
                unEmpleadoNew = em.getReference(unEmpleadoNew.getClass(), unEmpleadoNew.getNroDoc());
                categoriaEmp.setUnEmpleado(unEmpleadoNew);
            }
            categoriaEmp = em.merge(categoriaEmp);
            if (unEmpleadoOld != null && !unEmpleadoOld.equals(unEmpleadoNew)) {
                unEmpleadoOld.setUnaCategoriaEmp(null);
                unEmpleadoOld = em.merge(unEmpleadoOld);
            }
            if (unEmpleadoNew != null && !unEmpleadoNew.equals(unEmpleadoOld)) {
                CategoriaEmp oldUnaCategoriaEmpOfUnEmpleado = unEmpleadoNew.getUnaCategoriaEmp();
                if (oldUnaCategoriaEmpOfUnEmpleado != null) {
                    oldUnaCategoriaEmpOfUnEmpleado.setUnEmpleado(null);
                    oldUnaCategoriaEmpOfUnEmpleado = em.merge(oldUnaCategoriaEmpOfUnEmpleado);
                }
                unEmpleadoNew.setUnaCategoriaEmp(categoriaEmp);
                unEmpleadoNew = em.merge(unEmpleadoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = categoriaEmp.getNombre();
                if (findCategoriaEmp(id) == null) {
                    throw new NonexistentEntityException("The categoriaEmp with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CategoriaEmp categoriaEmp;
            try {
                categoriaEmp = em.getReference(CategoriaEmp.class, id);
                categoriaEmp.getNombre();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The categoriaEmp with id " + id + " no longer exists.", enfe);
            }
            Empleado unEmpleado = categoriaEmp.getUnEmpleado();
            if (unEmpleado != null) {
                unEmpleado.setUnaCategoriaEmp(null);
                unEmpleado = em.merge(unEmpleado);
            }
            em.remove(categoriaEmp);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CategoriaEmp> findCategoriaEmpEntities() {
        return findCategoriaEmpEntities(true, -1, -1);
    }

    public List<CategoriaEmp> findCategoriaEmpEntities(int maxResults, int firstResult) {
        return findCategoriaEmpEntities(false, maxResults, firstResult);
    }

    private List<CategoriaEmp> findCategoriaEmpEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CategoriaEmp.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CategoriaEmp findCategoriaEmp(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CategoriaEmp.class, id);
        } finally {
            em.close();
        }
    }

    public int getCategoriaEmpCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CategoriaEmp> rt = cq.from(CategoriaEmp.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
